/*****************************************************************************
 * Copyright (c) 2010 -, Anders All rights reserved
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *  Contact: Anders Uddenberg (anders.uddenberg@gmail.com)
 * 
 *
 ****************************************************************************/
package org.easypr.organizer.client.view;

import java.util.ArrayList;

import org.easypr.organizer.client.presenter.SearchResultPresenter;
import org.easypr.organizer.shared.MyContact;
import org.easypr.organizer.shared.MyUser;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

// TODO: Auto-generated Javadoc
/**
 * The Class SearchResultView.
 */
public class SearchResultView extends Composite implements SearchResultPresenter.Display {

	/** The container. */
	FlowPanel container;
	

	/**
	 * Instantiates a new menu view.
	 */
	public SearchResultView() {
		container = new FlowPanel();
		initWidget(container);
	}

	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.SearchResultPresenter.Display#setData(java.util.ArrayList, org.easypr.organizer.shared.MyUser)
	 */
	public void setData(ArrayList<MyContact> contacts, MyUser user) {
		container.clear();
		
		HorizontalPanel header = new HorizontalPanel();
		header.setStyleName("content-header");
		
		Label lbl = new Label("Search result");
		lbl.setStyleName("header-label");
		
		header.add(lbl);
		container.add(header);
		
		
		FlowPanel body = new FlowPanel();
		body.setStyleName("content-body");

		if(contacts == null || contacts.size() == 0) {
			Label notFound = new Label("No contacts were found");
			notFound.addStyleName("bold");
			body.add(notFound);
		} else {
			
			
			
			FlexTable table = new FlexTable();
			
			

			
			int cell = 0;
			int row = 0;
			int breakpoint = contacts.size()/2+1;
			for(int i=0; i<contacts.size(); i++) {
				
				FlowPanel item = new FlowPanel();
				item.addStyleName("contact-item");
				Label name = new Label(contacts.get(i).getName());
				name.addStyleName("contact-name");
				item.add(name);
				if(contacts.get(i).getEmail().length() > 0) {
					item.add(new Label(contacts.get(i).getEmail()));
				}
				if(contacts.get(i).getCellphone().length() > 0) {
					item.add(new Label("Cell: " + contacts.get(i).getCellphone()));
				}
				if(contacts.get(i).getWorkphone().length() > 0) {
					item.add(new Label("Workphone: " + contacts.get(i).getWorkphone()));
				}
				if(contacts.get(i).getWorkplace().length() > 0) {
					item.add(new Label("Workplace: " + contacts.get(i).getWorkplace()));
				}
				
				// TODO: Add edit and delete links
				
				if(i == breakpoint) {
					cell++;
					row = 0;
				}
				table.getCellFormatter().setVerticalAlignment(row, cell, HasVerticalAlignment.ALIGN_TOP);
				table.setWidget(row, cell, item);
				row++;
				
			}
			
			body.add(table);
			
			
		}
		container.add(body);
	}

	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.SearchResultPresenter.Display#asWidget()
	 */
	@Override
	public Widget asWidget() {
		return this;
	}
}
